package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.model.SysPost;
import com.ailanyin.admin.modules.system.service.SysPostService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台岗位管理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 14:41
 */
@RestController
@RequestMapping("/system/post")
@Api(tags = "后台岗位管理")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    @PostMapping
    @ApiOperation(value = "添加岗位")
    @PreAuthorize("hasAuthority('system:post:add')")
    public AjaxResult insertPost(@Validated @RequestBody SysPost post) {
        sysPostService.insertPost(post);
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation(value = "修改岗位")
    @PreAuthorize("hasAuthority('system:post:edit')")
    public AjaxResult updatePost(@Validated @RequestBody SysPost post) {
        sysPostService.updatePost(post);
        return AjaxResult.success();
    }

    @DeleteMapping("/{postIds}")
    @ApiOperation(value = "删除岗位")
    @PreAuthorize("hasAuthority('system:post:remove')")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        sysPostService.deletePostByIds(postIds);
        return AjaxResult.success();
    }

    @GetMapping("/optionselect")
    @ApiOperation(value = "获取岗位选择框列表")
    public AjaxResult optionSelect() {
        return AjaxResult.success(sysPostService.selectPostAll());
    }

    @GetMapping(value = "/{postId}")
    @ApiOperation(value = "获取岗位具体信息")
    @PreAuthorize("hasAuthority('system:post:query')")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return AjaxResult.success(sysPostService.selectPostById(postId));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取岗位分页列表")
    @PreAuthorize("hasAuthority('system:post:list')")
    public CommonPage list(SysPost post,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysPost> list = sysPostService.selectPostList(post);
        return CommonPage.restPage(list);
    }
}
